package com.sgb.kjwl.viewmodel.manage;

import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.manage.IPDetailEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.ManageRepertory;
import com.sgb.kjwl.view.ui.fragment.manage.IntellectualPropertyForWebsite;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class IntellectualPropertyForWebsiteModel extends BaseViewModel {

    private IntellectualPropertyForWebsite fragment;
    private ManageRepertory repertory;
    private Map<String, Object> map = new HashMap<>();

    public IntellectualPropertyForWebsiteModel(IntellectualPropertyForWebsite fragment, ManageRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    public void getIntellectualPropertyList(String siteNo, int siteType, Map map) {
        map.put("companyNo", siteNo);
        //map.put("siteNo", siteNo);
        //map.put("siteType", siteType);
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getManageAPI().getTrademarkListForShop(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<IPDetailEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<IPDetailEntity>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    if (data.getData() != null && data.getData().getList() != null){
                        fragment.showList(data.getData().getList());
                    } else {
                        fragment.showList(new ArrayList<>());
                    }
                } else {
                    fragment.showList(new ArrayList<>());
                }
            }
        });
    }

    //商标类型
    public void getTrademarkType() {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getManageAPI().getTrademarkType().enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    MToast.showToast(fragment.getContext(), "暂无数据");
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<PickSelectEntity> list = new ArrayList<>();
                    list.addAll(response.body().getData());
                    PickSelectEntity entity = new PickSelectEntity();
                    entity.setNo("");
                    entity.setProjectscale("不限");
                    list.add(0, entity);
                    fragment.setTypeDatas(list,false);
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }

    //专利类型
    public void getPatentType() {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getManageAPI().getPatentType().enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    MToast.showToast(fragment.getContext(), "暂无数据");
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<PickSelectEntity> list = new ArrayList<>();
                    list.addAll(response.body().getData());
                    PickSelectEntity entity = new PickSelectEntity();
                    entity.setNo("");
                    entity.setProjectscale("不限");
                    list.add(0, entity);
                    fragment.setTypeDatas(list, false);
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }
    //版权类型
    public void getRightType() {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getManageAPI().getRightType().enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    MToast.showToast(fragment.getContext(), "暂无数据");
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<PickSelectEntity> list = new ArrayList<>();
                    list.addAll(response.body().getData());
                    PickSelectEntity entity = new PickSelectEntity();
                    entity.setNo("");
                    entity.setProjectscale("不限");
                    list.add(0, entity);
                    fragment.setTypeDatas(list, false);
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }

    //行业类型
    public void getTradeCategory() {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getManageAPI().getTypeList().enqueue(new Callback<BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>>> call, Response<BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<PickSelectEntity<List<PickSelectEntity>>> list = new ArrayList<>();
                    list.add(new PickSelectEntity("", "不限"));
                    list.addAll(response.body().getData());
                    fragment.setmTradeCategoryData(list);
                } else {
                    MToast.showLongToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

    }

    //领域分类
    public void getFieldCategory() {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getManageAPI().getFieldClassifyType().enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<PickSelectEntity> list = response.body().getData();
                    PickSelectEntity pickSelectEntity = new PickSelectEntity();
                    pickSelectEntity.setProjectscale("不限");
                    pickSelectEntity.setNo("");
                    list.add(0, pickSelectEntity);
                    fragment.setmFieldCategoryData(list);
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //专利状态
    public void getPatentState() {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getManageAPI().getPatentStatus().enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<PickSelectEntity> list = response.body().getData();
                    PickSelectEntity pickSelectEntity = new PickSelectEntity();
                    pickSelectEntity.setProjectscale("不限");
                    pickSelectEntity.setNo("");
                    list.add(0, pickSelectEntity);
                    fragment.setmPatentStateData(list);
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //获取城市数据
    public void getCityData(String province) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getAreaSecond(province).enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK) && response.body().getData() != null) {
                    List<PickSelectEntity> list = response.body().getData();
                    PickSelectEntity pickSelectEntity = new PickSelectEntity();
                    pickSelectEntity.setProjectscale("不限");
                    pickSelectEntity.setNo("");
                    list.add(0, pickSelectEntity);
                    fragment.setCityDatas(list);
                } else {
                    MToast.showLongToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //获取省数据
    public void getProvinceData() {
        Map map = new HashMap();
        map.put("areaCode", repertory.getAreaCode());
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getLocationNew(map).enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK) && response.body().getData() != null) {
                    List<PickSelectEntity> list = response.body().getData();
                    list.get(0).setProjectscale("不限");
                    fragment.setProvinceDatas(list);
                } else {
                    MToast.showLongToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //获取城市数据
    public void getCityData2(String province) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getAreaSecond(province).enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK) && response.body().getData() != null) {
                    List<PickSelectEntity> list = response.body().getData();
                    PickSelectEntity pickSelectEntity = new PickSelectEntity();
                    pickSelectEntity.setProjectscale("不限");
                    pickSelectEntity.setNo("");
                    list.add(0, pickSelectEntity);
                    fragment.setCityDatas2(list);
                } else {
                    MToast.showLongToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //获取省数据
    public void getProvinceData2() {
        Map map = new HashMap();
        map.put("areaCode", repertory.getAreaCode());
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getLocationNew(map).enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK) && response.body().getData() != null) {
                    List<PickSelectEntity> list = response.body().getData();
                    list.get(0).setProjectscale("不限");
                    fragment.setProvinceDatas2(list);
                } else {
                    MToast.showLongToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public MutableLiveData<List<PickSelectEntity>> mMenus = new MutableLiveData<>();
    public void getSecondMenus(String siteNo, String siteType){
        map.put("siteNo", siteNo);
        map.put("siteType", siteType);
        map.put("type", 3);//3知识产权
        APIManager.getInstance().getWebsiteInfoAPI().getWebsiteSecondMenu(map).enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data) {
                if (data.getData() != null){
                    mMenus.postValue(data.getData());
                }
            }
        });
    }

}
